#define GPHCON      (*((volatile unsigned long *)0x56000070))
#define ULCON0      (*((volatile unsigned long *)0x50000000))
#define UCON0       (*((volatile unsigned long *)0x50000004))
#define UBRDIV0     (*((volatile unsigned long *)0x50000028))
#define UTRSTAT0    (*((volatile unsigned long *)0x50000010))
#define UTXH0       (*((volatile unsigned long *)0x50000020))
#define URXH0       (*((volatile unsigned long *)0x50000024))

#define PCLK 50000000
#define BUAD 115200

void uart_init()
{
    //1.配置引脚功能
    GPHCON &= ~(0xF << 4);
    GPHCON |= (0xA << 4);

    //2.1 设置数据格式,数据位8位，1位停止位，无校验
    ULCON0 |= (3 << 0);

    //2.2 设置工作模式,收发都使用轮询
    UCON0 |= (9 << 0);

    //3.设置波特率
    UBRDIV0 = (int)(PCLK/(BUAD*16))-1;
}

void putc(unsigned char ch)
{
    while (!(UTRSTAT0 & (1<<2)));
	UTXH0 = ch;  
}


unsigned char getc(void)
{
    unsigned char ret = 0;

	while (!(UTRSTAT0 & (1<<0)));
	// 取数据
	ret = URXH0;  
    
	if ( (ret == 0x0D) || (ret == 0x0A) ) {
	    putc(0x0D);
	    putc(0x0A);	
    } else {
	    putc(ret);
    }
        return ret;
}

void send_string(unsigned char *str)
{
	while(*str){
		putc(*str++);	
	}
}

